﻿using Hzt_WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebHzt_Api.Models.Database;
using WebHzt_Api.service;

namespace WebHzt_Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DataController : Controller
    {
        private readonly IDatabase _redis;
        private readonly Hzt_WebApiContext _db;
        private readonly IDateService _dateService;
        public DataController(IDateService dateService, Hzt_WebApiContext db,RedisHelper client)
        {
            _redis = client.GetDatabase();
            _dateService = dateService;
            _db = db;
        }
        /// <summary>
        /// 获取投票项目列表
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public Datum GetDataCateGory(string Category)
        //{
        //    var Data = _db.Data.FirstOrDefault(x => x.DataId == DataId);
        //    return Data;
        //}
        [HttpPost]
        public IActionResult SkipDemo(string Category)
        {

            ///跳过skip不要前几条
            //var list1 = _db.Data.OrderBy(x => x.Category).Skip(1).ToList();
            ////Take select Top 1 * category
            //var list2 = _db.Data.OrderBy(x => x.Category).Take(1).ToList();
            //3-12
            //page:pageIndex
            var dates = _db.Data.Where(x => x.Category == Category);
            var pageIndex = 1;//页码 第几页
            var pagesize = 3;//一页多少条数据
            var list4 = dates
                .OrderBy(x => x.Category)
                .Skip((pageIndex - 1) * pagesize)
                .Take(pagesize)
                .ToList();
            return new ObjectResult(list4);
        }
        /// <summary>
        /// 投票接口
        /// </summary>
        /// <param name="VoteId"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string AddVoteRedis1(int VoteId)
        {
            //添加商品进入购物车
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var hashKey = "votee_" + userId;
            //对购物车数量增加 如果哈希没有就添加后再加一
            _redis.HashIncrement(hashKey, VoteId);
            return "投票成功！";
        }
    }
}
